package cqut.wys.crawler;

import cqut.wys.utils.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by wuyoushan on 2017/11/20.
 */
public class JsoupCrawler {

    public static void main(String[] args) throws IOException {
        getAutoHomeByJsoup();
    }

    /**
     * 爬取汽车之家 美人生活秀
     */
    private static void getAutoHomeByJsoup() throws IOException {

        AtomicInteger atomicInteger = new AtomicInteger(0);

        // 首先建立主目录
        FileUtils.makeDir(FileUtils.picDir);
        for (int i=1;i<9;i++) {
            Document doc = null;
            try {
                String str="https://club.autohome.com.cn/jingxuan/292/"+i;
                doc = Jsoup.connect(str).get();
            } catch (IOException e) {
                e.printStackTrace();
            }

            // 选择class为vector_listbox容器
            Elements elementClass = doc.select(".content");

            // 在容器中选择a链接，用于进入相册
            Elements elements = elementClass.select("img");
            for (Element element : elements) {
                String title = element.parent().attr("title");
                String fileName = atomicInteger.incrementAndGet() + ".jpg";
                String imgSrc = element.attr("data-original");

                if (imgSrc == "") {
                    continue;
                }

                System.out.println(title);
                System.out.println("==============");
                System.out.println("http:" + imgSrc);
//            String newImgSrc = "http://club1.autoimg.cn/album/g10/M14/E7/B9/userphotos/2017/11/17/12/wKgH4FoOa4aAIhVfAAFCTZZDoV4206.jpg";
                String newImgSrc = "http:" + imgSrc;
                FileUtils.downloadPic(FileUtils.getInputStream(newImgSrc), fileName, FileUtils.picDir);
            }
        }
    }
}
